#
# Copyright 2008 Spencer Davis <SpencerDavis91@gmail.com>
# Copyright 2008 Charles Connell <charles@connells.org>
#
# Recuerde is a simple todo list application for GNOME.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://gnu.org/licenses/.>
#

import wx

class TrayIcon(wx.TaskBarIcon):
    """Tray icon implemented for Recuerde."""
    def __init__(self, mainWindow):
        """Set up the tray icon."""
        wx.TaskBarIcon.__init__(self)

        # Remember main window if we need to open or close it
        self.mainWindow = mainWindow

        # Set the icon from file
        icon = wx.Icon('Recuerde_tray.ico', wx.BITMAP_TYPE_ICO)
        self.SetIcon(icon, "Recuerde running")

        # If we are clicked on, iconify or de-iconify the main window
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.Iconify)

    def CreatePopupMenu(self):
        """Show the context menu for the tray icon"""
        # Create the menu
        menu = wx.Menu()
        menu.Append(wx.ID_EXIT, "&Quit"," Terminate the program")
        # Bind exit button to OnExit
        wx.EVT_MENU(self, wx.ID_EXIT, self.OnExit)
        # Return menu to caller
        return menu

    def Iconify(self, event):
        """Make the window appear and disappear."""
        # If window is iconized (minimized), bring it up
        if self.mainWindow.IsIconized():
            self.mainWindow.Iconize(False)
        # If window is not mapped onto screen at all (screen or taskbar), make it appear
        if not self.mainWindow.IsShown():
            self.mainWindow.Show(True)
            self.mainWindow.Raise()
        # If window is deiconized, make it completely disappear
        if not self.mainWindow.IsIconized():
            self.mainWindow.Hide()

    def OnExit(self,e):
        """Quit the app."""
        wx.Exit()

